/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mabi.modular.runtime.module;

import com.mabi.modular.install.modules.IModuleDefinition;
import com.mabi.modular.runtime.module.contributions.actions.AbstractMabiAction;
import com.mabi.modular.runtime.module.contributions.views.ViewImpl;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Justo Disla
 */
public class RuntimeModuleFactory {
    private static RuntimeModuleFactory instance;

    public static RuntimeModuleFactory getDefault(){
        if(null == instance){
            instance = new RuntimeModuleFactory();
        }
        return instance;
    }
    
    /**
     * Builds a RuntimeModule from the given IModuleDefinition
     * @param module
     * @return 
     */
    public  RuntimeMabiModule buildRuntimeModule(final IModuleDefinition module) throws InstantiationException, IllegalAccessException {
        
        Rtm rtm = new Rtm();
        List<Class<ViewImpl>> views = module.getViews();
        List<ViewImpl> moduleViews = new ArrayList();
        for (Class<ViewImpl> vClass : views) {
            ViewImpl newInstance = vClass.newInstance();
            newInstance.setModuleName(module.getModuleName());
            moduleViews.add(newInstance);
        }
        
        rtm.views = moduleViews;
        
        return rtm;
    }
    
    private class Rtm implements RuntimeMabiModule{

        public List<ViewImpl> views;
        
        @Override
        public List<ViewImpl> getViews() {
            return views;
        }

        @Override
        public List<ViewImpl> getAutoloadViews() {
            List<ViewImpl> autoLoadViews = new ArrayList();
            for (ViewImpl view : views) {
                if(view.isAutoLoad()){
                    autoLoadViews.add(view);
                }
            }
            return autoLoadViews;
        }

        @Override
        public List<AbstractMabiAction> getActions() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public List<MabiService> getServices(String searchPattern) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getModuleName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getModuleDescription() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getModuleVersion() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getModuleJndiKey() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    
}
